/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.plugins  
 */
package com.apache.bid.service.plugins;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.Constant;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.common.cache.BidCacheManager;
import com.apache.bid.entity.BidApply;
import com.apache.bid.entity.BidItem;
import com.apache.bid.entity.BidMaxContent;
import com.apache.bid.manager.BidApplyManager;
import com.apache.bid.manager.BidItemManager;
import com.apache.bid.manager.BidMaxContentManager;
import com.apache.bid.manager.BidPubManager;
import com.apache.cache.util.Validator;

/**
 * description:  统一下删除标的插件
 * @author Hou Dayu 创建时间：2016-3-23  
 * 
 */
public class UnityDelItemPluginImpl implements PluginConnector {

	protected Logger log = Logger.getLogger(getClass());
	protected BidItemManager bidItemManager;
	protected BidPubManager bidPubManager;
	protected BidApplyManager bidApplyManager;
	protected BidMaxContentManager bidMaxContentManager;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.zbxsoft.api.manager.PluginConnector#execute(com.zbxsoft.api.vo.ParamsVo)  
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Object execute(ParamsVo param) throws Exception {
		ResultEntity entity = new ResultEntity();
		String itemId = Validator.getDefaultStr(param.getInfoId(), String.valueOf(param.getParams("infoId")));
		String seId = String.valueOf(param.getParams("sessionId"));
		if (Validator.isNull(itemId) && Validator.isNull(seId)) {
			entity.setResult("false");
			entity.setMessage("缺少必要参数，程序执行失败");
			return entity;
		}
		ParamsVo vo = new ParamsVo();
		if (Validator.isNotNull(itemId)) {
			vo.setInfoId(itemId);
		} else {
			vo.setParams("sessionId", param.getParams("sessionId"));
		}
		BidItem item = (BidItem) bidItemManager.getInfoById(vo);
		boolean mark = bidItemManager.deleteInfo(vo);
		if (mark) {
			if ("0".equals(item.getSessionId())) {
				ParamsVo pvo = new ParamsVo();
				pvo.setParams("itemId", itemId);
				bidPubManager.deleteInfo(pvo);
				BidMaxContent mct = new BidMaxContent();
				mct.setObjId(itemId);
				mct.setObjType("item");
				pvo.setObj(mct);
				bidMaxContentManager.deleteInfo(pvo);
			}
			if (Constant.SETYPE_HD.equals(item.getSessionType())) {//类型为活动的，删除时要删除竞买人等信息
				BidApply bidApply = new BidApply();
				bidApply.setItemId(itemId);
				vo.setObj(bidApply);
				bidApplyManager.deleteInfo(vo);
			}
			BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("item");
			cache.removeCacheObject(BidCacheManager.item_prefix + itemId);
		}
		entity.setEntity(mark);
		entity.setResult("true");
		entity.setMessage("标的删除成功");
		return entity;
	}

	public void setBidItemManager(BidItemManager bidItemManager) {
		this.bidItemManager = bidItemManager;
	}

	public void setBidPubManager(BidPubManager bidPubManager) {
		this.bidPubManager = bidPubManager;
	}

	public void setBidApplyManager(BidApplyManager bidApplyManager) {
		this.bidApplyManager = bidApplyManager;
	}

	public void setBidMaxContentManager(BidMaxContentManager bidMaxContentManager) {
		this.bidMaxContentManager = bidMaxContentManager;
	}

}
